package com.js.mytransitiondemo.shareElement;

// Created by JS on 2021/7/6.

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.js.mytransitiondemo.R;
import com.js.mytransitiondemo.adapter.ShareElementListAdapter;
import com.js.mytransitiondemo.databinding.ActivityShareElementBinding;

import java.util.ArrayList;
import java.util.List;

public class ShareElementActivity extends AppCompatActivity {

    private ActivityShareElementBinding binding;
    private List<Integer> mListData = new ArrayList<>();
    private ShareElementListAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityShareElementBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        init();
    }

    private void init() {
        initListData();
        mAdapter = new ShareElementListAdapter(this, mListData, (view, url) -> {
            Intent intent = new Intent(this, ShareElementTwoActivity.class);
            intent.putExtra("url", url);
            startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this, view, "shareImg").toBundle());
        });
        binding.list.setLayoutManager(new GridLayoutManager(this, 2));
        binding.list.setAdapter(mAdapter);

        binding.img5.setOnClickListener(v -> {
            Pair<View, String> one = new Pair<>(binding.img5, "shareImg5");
            Pair<View, String> two = new Pair<>(binding.img6, "shareImg6");
            Intent intent = new Intent(this, ShareElementThreeActivity.class);
            intent.putExtra("type", 0);
            ActivityOptions activityOptions = ActivityOptions.makeSceneTransitionAnimation(this, one, two);
            startActivity(intent, activityOptions.toBundle());
        });
        binding.img6.setOnClickListener(v -> {
            Pair<View, String> one = new Pair<>(binding.img5, "shareImg5");
            Pair<View, String> two = new Pair<>(binding.img6, "shareImg6");
            Intent intent = new Intent(this, ShareElementThreeActivity.class);
            ActivityOptions activityOptions = ActivityOptions.makeSceneTransitionAnimation(this, one, two);
            startActivity(intent, activityOptions.toBundle());
        });
    }

    private void initListData() {
        mListData.add(R.mipmap.img1);
        mListData.add(R.mipmap.img2);
        mListData.add(R.mipmap.img3);
        mListData.add(R.mipmap.img4);
    }
}
